package com.wlz.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author wlz
 * @date 2023-01-04  2:24 下午
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class DemoController {

    @Qualifier("getExecutor")
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/demo")
    public String demo(@RequestParam(value = "name")String name) {
        log.info("demo info: {}", name);
        log.warn("demo warn: {}", name);
        log.error("demo error: {}", name);
        return "hello " + name;
    }

    @RequestMapping("/demo2")
    public String demo2(@RequestParam(value = "name")String name) {
        log.info("demo2 info: {}", name);
        log.warn("demo2 warn: {}", name);
        log.error("demo2 error: {}", name);

        new Thread(new Runnable() {
            @Override
            public void run() {
                log.info("demo2 thread info: {}", name);
                log.warn("demo2 thread warn: {}", name);
                log.error("demo2 thread error: {}", name);
            }
        }).start();

        return "hello " + name;
    }


    @RequestMapping("/demo3")
    public String demo3(@RequestParam(value = "name")String name) {
        log.info("demo3 info: {}", name);
        log.warn("demo3 warn: {}", name);
        log.error("demo3 error: {}", name);

        for(int i=0;i<2;i++)
        {
            //子线程日志
            threadPoolTaskExecutor.execute(()->{
                log.info("demo3 thread info: {}", name);
                log.warn("demo3 thread warn: {}", name);
                log.error("demo3 thread error: {}", name);
            });
        }

        return "hello " + name;
    }

    @RequestMapping("/demo4")
    public String demo4(@RequestParam(value = "name")String name) {
        log.info("demo4 info: {}", name);
        log.warn("demo4 warn: {}", name);
        log.error("demo4 error: {}", name);

        ResponseEntity<String> exchange = restTemplate.exchange("http://127.0.0.1:9993/approval", HttpMethod.GET, null, String.class);
        log.info("demo4 exchange : {}", exchange.getBody());
        return "hello " + name;
    }


}
